<?php

namespace app\modules\backend\controllers;

use Yii;
use app\models\Land;
use app\models\searchs\LandSearch;
use app\models\Equip;
use app\models\searchs\EquipSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\data\ActiveDataProvider;

/**
 * LandController implements the CRUD actions for Land model.
 */
class LandController extends Controller
{
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * Lists all Land models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new LandSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Land model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        $searchModel = new EquipSearch();
        $landModel = $this->findModel($id);
        $query = Equip::find()->Where([ 'land_id' => $landModel->id ])
                              ->andWhere([ 'is_delete' => 0 ]);
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        return $this->render('view', [
            'model' => $landModel,
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider
        ]);
    }

    /**
     * Creates a new Land model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate($id=0)
    {
        $model = new Land();
        if($id!=0){
            $model->company_id=$id;
        }
        if( $model->load(Yii::$app->request->post()) ) {
            //caculate the code of the equip.
            $str = "LNS";
            $iCount = Land::find()->max('id') + 1;
            $strTmp = strval($iCount);
            $lNum = strlen($strTmp);
            if ($lNum < 2)
                $strTmp = '0'.$strTmp;
            $model->code = $str.$strTmp;

            if ($model->save())
                return $this->redirect(['view', 'id' => $model->id]);
            else
                return $this->render('create', [
                    'model' => $model,
                ]);
        }
        else
            return $this->render('create', [
                'model' => $model,
            ]);
    }

    /**
     * Updates an existing Land model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing Land model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the Land model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Land the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Land::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
